﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UserQuanXianManager
{
	public partial class AddNewUser : Form
	{
		public AddNewUser()
		{
			InitializeComponent();
		}

		private void bt_OK_Click(object sender, EventArgs e)
		{
			
			using (var context = new blackbirdEntities())
			{
				var username = tbusername.Text.Trim();
				var existed = context.user.FirstOrDefault(v => v.Name == username);
				if (existed != null)
				{
					MessageBox.Show("User already existed");
					return;
				}

				contactor newContactor = new contactor();
				newContactor.RealName = tbrealname.Text;
				newContactor.Address = tbAdress.Text;
				newContactor.Zipcode = tbzipcode.Text;
				newContactor.Email = tbemail.Text;
				newContactor.Tel = tbTel.Text;
				newContactor.Phone = tbphone.Text;

				context.contactor.AddObject(newContactor);
				context.SaveChanges();

				user newUser = new user();
				newUser.idUser = DateTime.Now.ToFileTime().ToString();
				newUser.Contactor_idContactor = newContactor.idContactor;
				newUser.Name = username;
				newUser.Password = tbpassword.Text.Trim();
				newUser.DisplayName = tbdisplayname.Text;

				context.user.AddObject(newUser);
				context.SaveChanges();

			}

			this.DialogResult = System.Windows.Forms.DialogResult.OK;

			this.Close();
		}
	}
}
